﻿using System;
using System.Collections.Generic;
using System.Reflection;
using AnjLab.SyncIT.Core.Api;
using NUnit.Framework;

namespace AnjLab.SyncIT.Core.Tests.Data._1C
{
    [TestFixture]
    public class Test1C
    {
        [Test]
        public void TestGetDocumentsFrom1C()
        {
            try
            {
                var v81comConnector = Type.GetTypeFromProgID("V81.ComConnector");
                var v81             = Activator.CreateInstance(v81comConnector);
                Object[] arguments  = { @"File=""C:\Users\mix\Documents\1C\DemoEnterprise"";Usr=""Федоров (администратор)"";" };
                var x               = v81comConnector.InvokeMember("Connect", 
                    BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, v81, arguments);

                var objDocs_      = GetObjectProperty(v81, x, "Документы");
                var docEvent_     = GetObjectProperty(v81, objDocs_, "Событие");
                var docSelection_ = InvokeObjectMethod(v81, docEvent_, "Выбрать", new Object[] {});

                while ((bool) InvokeObjectMethod(v81, docSelection_, "Следующий", new Object[] {}))
                {
                    var eventDeletionMark_ = GetObjectProperty(v81, docSelection_, "ПометкаУдаления");
                    if (!(bool) eventDeletionMark_)
                    {
                        var eventID_ = GetObjectProperty(v81, docSelection_, "Номер");

                        var eventPriorityObj_ = GetObjectProperty(v81, docSelection_, "Важность");
                        var eventPriority_ = InvokeObjectMethod(v81, x, "XMLString", new Object[] {eventPriorityObj_});

                        var eventStateObj_ = GetObjectProperty(v81, docSelection_, "СостояниеСобытия");
                        var eventState_ = InvokeObjectMethod(v81, x, "XMLString", new Object[] {eventStateObj_});

                        var eventDescription_ = GetObjectProperty(v81, docSelection_, "ОписаниеСобытия");
                        var eventContent_ = GetObjectProperty(v81, docSelection_, "СодержаниеСобытия");

                        var eventResponsibleObj_ = GetObjectProperty(v81, docSelection_, "Ответственный");
                        var eventResponsible_ = GetObjectProperty(v81, eventResponsibleObj_, "Description");

                        var eventBeginning_ = GetObjectProperty(v81, docSelection_, "НачалоСобытия");
                        var eventEnd_ = GetObjectProperty(v81, docSelection_, "ОкончаниеСобытия");

                        Console.WriteLine("Информация о событии:");
                        Console.WriteLine("ID: " + eventID_);
                        Console.WriteLine("Важность (приоритет): " + eventPriority_);
                        Console.WriteLine("Состояние: " + eventState_);
                        Console.WriteLine("Описание: " + eventDescription_);
                        Console.WriteLine("Содержание: " + eventContent_);
                        Console.WriteLine("Ответственный: " + eventResponsible_);
                        Console.WriteLine("Начало: " + eventBeginning_);
                        Console.WriteLine("Окончание: " + eventEnd_);
                        Console.WriteLine("--------------------");
                    }
                }
            }
            catch (Exception x)
            {
                //Library.Log.Error("Can't establish connection to 1C", x);
                Console.WriteLine(x);
            }
        }

        public object GetObjectProperty(object v81, object refObject, string propertyName)
        {
            return v81.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, refObject, null);
        }

        public object SetObjectProperty(object v81, object refObject, string propertyName, Object[] PropertyValue)
        {
            return v81.GetType().InvokeMember(propertyName, BindingFlags.SetProperty , null, refObject, PropertyValue);
        }

        public object InvokeObjectMethod(object v81, object refObject, string methodName, Object[] parameters)
        {
            return v81.GetType().InvokeMember(methodName, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, refObject, parameters);
        }

        [Test]
        public void TestSetPropertyObject()
        {
            var v81comConnector = Type.GetTypeFromProgID("V81.ComConnector");
            var v81 = Activator.CreateInstance(v81comConnector);
            Object[] arguments = { @"File=""C:\Users\mix\Documents\1C\DemoEnterprise"";Usr=""Федоров (администратор)"";" };
            var x = v81comConnector.InvokeMember("Connect",
                BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, v81, arguments);

            var objDocs_ = GetObjectProperty(v81, x, "Документы");
            var docEvent_ = GetObjectProperty(v81, objDocs_, "Событие");

            var v81Event = InvokeObjectMethod(v81, docEvent_, "НайтиПоНомеру", new Object[] { "00000004", DateTime.Now });

            var v81EventObject = InvokeObjectMethod(v81, v81Event, "ПолучитьОбъект", new Object[] { });

            SetObjectProperty(v81, v81EventObject, "ОписаниеСобытия", new Object[] { "STRING" });
            InvokeObjectMethod(v81, v81EventObject, "Записать", new Object[] {});
        }

    }
}
